import {
  mapGetters,
  mapMutations,
  mapActions,
  // mapActions
} from 'vuex';
import {
  playMode
} from "@/utils/config.js";
import {
  shuffle
} from "@/utils/shuffle.js";
import {
  likeMusic
} from '@/request/api'

// 全局检测是否有Mini播放器（给better-scroll设置bottom）
export const playListMixin = {
  computed: {
    ...mapGetters(['playList'])
  },
  mounted() {
    this.handlePlayList(this.playList)
  },
  // keepalive组件更新以后
  activated() {
    this.handlePlayList(this.playList)
  },
  watch: {
    playList(newVal) {
      this.handlePlayList(newVal)
    }
  },
  methods: {
    handlePlayList() {
      //   组件内部必须实现这个方法，否则报错：
      throw new Error('需要在需要实现此方法，改变bs的bottom')
    }
  },
}


// 播放状态mixin:
export const playerMixin = {
  computed: {
    iconMode() {
      if (this.mode === playMode.sequence) {
        return "iconMode_Sq";
      } else if (this.mode === playMode.loop) {
        return "iconMode_Loop";
      } else {
        return "iconMode_Random";
      }
    },
    PlayModeTips() {
      if (this.mode === playMode.sequence) {
        return "顺序播放";
      } else if (this.mode === playMode.loop) {
        return "单曲循环";
      } else {
        return "随机播放";
      }
    },
    ...mapGetters(["sequenceList", "currentSong", "playList", "mode", "favoriteList", "userLikeList", "isLogin"]),
  },
  methods: {
    getFavoriteIcon(song) {
      // 非登录状态:
      if (!this.isLogin) {
        if (this.isFavorite(song)) {
          return 'iconlike'
        }
        return 'icondislike'
      } else {
        // 登录状态:
        if (this.isFavorite(song)) {
          return 'iconlike'
        }
        return 'icondislike'
      }

    },
    toggleFavorite(song) {
      // 非登录状态下切换:
      if (!this.isLogin) {
        if (this.isFavorite(song)) {
          this.deleteFavoriteList(song);
          this.$toast('已取消喜欢');
        } else {
          this.saveFavoriteList(song);
          this.$toast('已喜欢');
        }
      }
      // 登录下切换:
      else {
        if (this.isFavorite(song)) {
          likeMusic(song.id, {
            like: false
          }).then(res => {
            if (res.code === 200) {
              this.deleteLikeList(song)
              this.$toast('已取消喜欢');
              this.$refs.likeRef.classList.remove("iconlike");
              this.$refs.likeRef.classList.add("icondislike");
            }
          })
        } else {
          this.$toast('已喜欢');
          likeMusic(song.id, {
            like: true
          }).then(res => {
            this.saveLikeList(song)
            console.log(res);
            this.$refs.likeRef.classList.remove("icondislike");
            this.$refs.likeRef.classList.add("iconlike");
          })
        }
      }

    },
    isFavorite(song) {
      // 非登录状态:
      if (!this.isLogin) {
        // 离线的喜欢歌曲:
        const index = this.favoriteList.findIndex((item) => {
          return item.id === song.id
        })
        return index > -1
      } else {
        let islikeOnline = this.userLikeList.findIndex((item) => {
          return item.id === song.id
        });
        return islikeOnline > -1
      }
    },
    // 切换播放模式
    changeMode(hastips) {
      const mode = (this.mode + 1) % 3;
      this.setPlayMode(mode);
      if (hastips) {
        this.$toast({
          message: `切换播放模式:${this.PlayModeTips}`,
          duration: 800
        });
      }
      let list = null;
      if (mode === playMode.random) {
        // 获取被打乱的列表
        list = shuffle(this.sequenceList);
      } else {
        // 获取顺序列表
        list = this.sequenceList;
      }
      // 保证currentSong的id不变：
      this.resetCurrentIndex(list);
      this.setPlayList(list);
    },
    // 切换时候需要把currentIndex保存下来：
    resetCurrentIndex(list) {
      let index = list.findIndex(item => {
        return item.id === this.currentSong.id;
      });
      this.setCurrentIndex(index);
    },
    ...mapActions(['saveFavoriteList', 'deleteFavoriteList', 'deleteLikeList', 'saveLikeList']),
    ...mapMutations({
      setPlayingState: "SET_PLAYING_STATE",
      setCurrentIndex: "SET_CURRENT_INDEX",
      setPlayMode: "SET_PLAY_MODE",
      setPlayList: "SET_PLAYLIST",
    }),
  },
}
